//
//  SocialViewController.m
//  SXH
//
//  Created by Ryan on 16/6/24.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "SocialViewController.h"
#import "CommonService.h"
#import <ShareSDK/ShareSDK.h>


//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

//微信SDK头文件
#import "WXApi.h"

@interface SocialViewController ()
@property (weak, nonatomic) IBOutlet UIButton *qqBtn;
@property (weak, nonatomic) IBOutlet UIButton *wechatBtn;
@property (weak, nonatomic) IBOutlet UIButton *sinaBtn;
@property (weak, nonatomic) IBOutlet UILabel *qqLabel;
@property (weak, nonatomic) IBOutlet UILabel *wechatLabel;
@property (weak, nonatomic) IBOutlet UILabel *sinaLabel;

@end

@implementation SocialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getSocialState];
    
    UIView *view1 = [self.view viewWithTag:1000];
    UIView *view2 = [self.view viewWithTag:1001];
    UIView *view3 = [self.view viewWithTag:1002];
    
    /**
     *  检测第三方是否安装
     */
    if (![WXApi isWXAppInstalled] && [QQApi isQQInstalled]) {
        view1.hidden = YES;
        view2.transform = CGAffineTransformMakeTranslation(0, -44);
        view3.transform = CGAffineTransformMakeTranslation(0, -44);
    }
    if ([WXApi isWXAppInstalled] && ![QQApi isQQInstalled]) {
        view2.hidden = YES;
        view3.transform = CGAffineTransformMakeTranslation(0, -44);
    }
    if (![WXApi isWXAppInstalled] && ![QQApi isQQInstalled]) {
        view1.hidden = YES;
        view2.hidden = YES;
        view3.transform = CGAffineTransformMakeTranslation(0, -88);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getSocialState
{
    CommonService *service = [[CommonService alloc]init];
    [service getNetWorkData:nil Path:Bound_Social Successed:^(id entity) {
        NSDictionary *dic = [entity objectForKey:@"data"];
        if ([[dic objectForKey:@"qq_bound"]intValue]) {
            self.qqBtn.hidden = YES;
            self.qqLabel.hidden = NO;
        }
        else
        {
            self.qqBtn.hidden = NO;
            self.qqLabel.hidden = YES;
        }
        if ([[dic objectForKey:@"weichat_bound"]intValue]) {
            self.wechatBtn.hidden = YES;
            self.wechatLabel.hidden = NO;
        }
        else
        {
            self.wechatBtn.hidden = NO;
            self.wechatLabel.hidden = YES;
        }
        if ([[dic objectForKey:@"weibo_bound"]intValue])
        {
            self.sinaBtn.hidden = YES;
            self.sinaLabel.hidden = NO;
        }
        else
        {
            self.sinaBtn.hidden = NO;
            self.sinaLabel.hidden = YES;

        }
        [self showLoadingView:NO];

    } Failed:^(NSInteger errorCode, NSString *message) {
        [self showLoadingView:NO];

    }];
}

- (IBAction)associateQQ:(id)sender {
    [ShareSDK getUserInfo:SSDKPlatformTypeQQ onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        if (state == SSDKResponseStateSuccess)
        {
            
            NSLog(@"uid=%@",user.uid);
            NSLog(@"%@",user.credential);
            NSLog(@"token=%@",user.credential.token);
            NSLog(@"nickname=%@",user.nickname);
            [self associateWithType:QQLogin andSSDKUser:user];
        }
    }];
}
- (IBAction)associateWeChat:(id)sender {
    [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat];
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        NSLog(@"state = %ld",state);
        NSLog(@"user = %@",user);
        NSLog(@"error = %@",error);
        if (state == SSDKResponseStateSuccess)
        {
            [self associateWithType:WeChatLogin andSSDKUser:user];
        }
    }];
}


- (IBAction)associateWeiBo:(id)sender {
    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        NSLog(@"state = %ld",state);
        NSLog(@"user = %@",user.credential);
        
        NSLog(@"error = %@",error);
        
        if (state == SSDKResponseStateSuccess)
        {
            [self associateWithType:WeiBoLogin andSSDKUser:user];
            
        }
    }];

}

- (void)associateWithType:(SocialLoginType)type andSSDKUser:(SSDKUser*)user
{
    CommonService *service = [[CommonService alloc]init];
    NSDictionary *param = nil;
    [self showLoadingView:YES];
    if (type == QQLogin) {
        param = [NSDictionary dictionaryWithObjectsAndKeys:
                 @"1", @"third_class",
                 user.uid, @"uid",
                 nil];
    }
    if (type == WeChatLogin) {
        param = [NSDictionary dictionaryWithObjectsAndKeys:
                 @"2", @"third_class",
                 user.uid, @"uid",
                 nil];
    }    if (type == WeiBoLogin) {
        param = [NSDictionary dictionaryWithObjectsAndKeys:
                 @"3", @"third_class",
                 user.uid, @"uid",
                 nil];
    }
    [service getNetWorkData:param Path:BindSocial Successed:^(id entity) {
        [self getSocialState];
    } Failed:^(NSInteger errorCode, NSString *message) {
        [self showLoadingView:NO];

    }];
}
- (IBAction)doback:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
